<?php

class IndexController extends Zend_Controller_Action
{
    public function indexAction(){}

    public function aboutAction()
    {
        $this->view->headTitle('О компании');
    }

    public function usefulAction()
    {
        $this->view->headTitle('Полезная информация');
        $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Useful(), new Application_Model_Useful());

        if ((int) $this->_getParam('id')) {
            $this->view->isOneUseful = true;
            $this->view->usefuls = $mapper->getCollection(array(Application_Model_DbTable_Useful::FIELD_ID => (int) $this->_getParam('id')));
        } else {
            $this->view->usefuls = $mapper->getCollection(array(Application_Model_DbTable_Useful::FIELD_IS_ACTIVE => 1), 'useful_id DESC');
        }
     }

    public function galleryAction()
    {
        $this->view->headTitle('Галерея');
        $mapper = new Application_Model_Mapper(new Application_Model_DbTable_GalleryAlbums(), new Application_Model_GalleryAlbums());
        $this->view->albums = $mapper->getCollection();
    }

    public function photosAction()
    {
        $albumId = (int) $this->_getParam('id');

        if (!$albumId) {
            $this->_redirect('/gallery');
        }
        $mapperAlbum = new Application_Model_Mapper(new Application_Model_DbTable_GalleryAlbums(), new Application_Model_GalleryAlbums());
        $album = $mapperAlbum->getById($albumId);

        if (!$album) {
            $this->_redirect('/gallery');
        }

        $this->view->album = $album;
        $this->view->headTitle("Галерея | " . $this->view->album->getTitle());
        $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Gallery(), new Application_Model_Photo());
        $this->view->photos = $mapper->getCollection(array('album_id' => $albumId), Application_Model_DbTable_Gallery::FIELD_ID  . ' DESC');
    }

    public function contactsAction()
    {
        $this->view->headTitle('Контакты');
    }

    public function stockAction()
    {
        $this->view->headTitle('Акции');

        if ((int) $this->_getParam('id')) {
            $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Stock(), new Application_Model_Stock());
            $stock = $mapper->getById((int) $this->_getParam('id'));

            if ($stock && $stock->getIsActive()) {
                $this->view->stock = $stock;
                $this->render('one-stock');
            }
        }

    }

    public function loginAction()
    {
        if ($this->getRequest()->isPost()) {
            $result = $this->_helper->authorization($this->_getParam('login'), $this->_getParam('pass'));

            if ($result->isValid()) {
                Zend_Session::rememberMe(60 * 60 * 24 * 14);
                $this->_redirect('/admin/');
            }
        }
    }

    public function logoutAction()
    {
        $this->_helper->authorization->logout();
        $this->_redirect('/');
    }
}